﻿function convert() {

    locations = new Array;
    locations = document.getElementById("address").value.split("\n");

    var location = locations[0];
    cursor = 0;

    processAddress(location);

}

function convertNext() {

    cursor++;
    setCurrentLatLng(0, 0);
    setCurrentAddress("");

    document.getElementById("confirmNext").value = "processing " + cursor + " of " + locations.length;
    
    if (cursor >= locations.length) {
        alert("终于结束了");
        return;
    }

    var location = locations[cursor];
    processAddress(location);
}

function init() {
    geocoder = new GClientGeocoder();
    resultText = document.getElementById("result");
    initMap();
  
}

function initMap() {
    if (GBrowserIsCompatible()) {

        var lat = 36.5978891330702;   // zoom to the whole japan 
        var lng = 135.6591796875;
        var scale = 6;

        map = new GMap2(document.getElementById("mapN"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.enableDoubleClickZoom();
        map.continuousZoomEnabled();
        map.setCenter(new GLatLng(lat, lng), scale);

    } // GBrowserIsCompatible
}


function addResult(text) {
    resultText.value += text + "\n";

}

function setCurrentAddress(text) {
    currentAddress = text;
    document.getElementById("currentPlace").value = text;
}

function setCurrentLatLng(lat, lng) {
    currentLat = lat;
    currentLng = lng;
    document.getElementById("currentLatLng").value = "Lat: " + currentLat + ", Lng: " + currentLng;
}

function processAddress(location) {
    setCurrentAddress(location);

    if (geocoder) {
        geocoder.getLatLng(
                              location,
                           function(point) {
                               if (!point) {
                                   addResult("####not found,  " + location);
                                   window.setTimeout(function() {

                                       convertNext();
                                   }, 100);

                               } else {

                                   //setCurrentLatLng(point.lat(), point.lng());
                                   var t = "," + point.lat() + "," + point.lng() + "," + currentAddress;

                                   addResult(t);

                                   window.setTimeout(function() {

                                       convertNext();
                                   }, 100);
                                   // map.setCenter(point, 13);
                                   //var marker = new GMarker(point);
                                   //map.addOverlay(marker);
                                   //marker.openInfoWindowHtml(location);

                               } //end if 
                           } // end function 
                   );               // end getLatLng 
    }  // end if (geocoder)  */



}



function confirm() {
    var t = "," + currentLat + "," + currentLng + "," + currentAddress;

    addResult(t);
    convertNext();
    
}
